<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>64进制</title>
</head>
<body>
  <input type="text" id="input" > 
  <span id="result"></span>
  <div><button id="btn">转换成64进制</button></div>
  <script>
    const input = document.getElementById('input')
    const result = document.getElementById('result')
    document.getElementById('btn').onclick = function() {
      result.innerHTML = '转换结果为：' + to64(+input.value)
    }
    const SIGNS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/".split("");
    function to64(value = 0, radix = 64) {

      if(typeof value !== 'number' || value !== value) {
        alert('must be number')
        throw Error('must be number')
        return
      }
      // 正负符号
      let flag = ''
      if(value < 0) flag = '-'
      // 按正数方式转换
      value = Math.abs(value)

      let intPart = Math.floor(value)
      let decimalPart = value -  intPart
      // 整数部分转换
      let intPartArr = []
      while(intPart >= radix) {
        intPartArr.push(intPart % radix)
        intPart = Math.floor(intPart / radix)
      }
      intPartArr.push(intPart)
      intPartArr = intPartArr.map(item => SIGNS[item])
      // 小数部分转换
      let decimalPartArr = []
      let l = 0
      while(decimalPart !== 0 && l++ <= 20) {
        const curNum = decimalPart * radix
        const curIntPart = Math.floor(curNum)
        decimalPartArr.push(curIntPart)
        decimalPart = curNum - curIntPart
      }
      decimalPartArr = decimalPartArr.map(item => SIGNS[item])

      return flag + intPartArr.reverse().join('') + (!!decimalPartArr.length ? `.${decimalPartArr.join('')}` : '')
    }

  </script>
</body>
</html>